package com.neusoft.neps.controller;

import com.neusoft.neps.entity.user;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.neusoft.neps.method.UsersIO;

public class RegisterController {

    @FXML private TextField phoneField;
    @FXML private TextField nameField;

    @FXML private PasswordField passwordField;
    @FXML private PasswordField confirmPasswordField;
    @FXML private Button maleButton;
    @FXML private Button femaleButton;
    @FXML private Button backButton;
    @FXML private Button registerButton;
    @FXML private Button sureButton;

    private String gender = "男";

    @FXML
    private void initialize() {
        // 设置性别选择按钮样式
        maleButton.setStyle("-fx-background-color: #5F9EA0;");
        femaleButton.setStyle("-fx-background-color: #F5F5F5;");
    }

    @FXML
    private void handleMaleSelect() {
        gender = "男";
        maleButton.setStyle("-fx-background-color: #5F9EA0;");
        femaleButton.setStyle("-fx-background-color: #F5F5F5;");
    }

    @FXML
    private void handleFemaleSelect() {
        gender = "女";
        femaleButton.setStyle("-fx-background-color: #5F9EA0;");
        maleButton.setStyle("-fx-background-color: #F5F5F5;");
    }

    @FXML
    private void handleRegister() {
        // 验证表单数据
        if (phoneField.getText().isEmpty() || nameField.getText().isEmpty() ||
                 passwordField.getText().isEmpty() ||
                confirmPasswordField.getText().isEmpty()) {

            showAlert("注册失败", "请填写所有必填项");
            return;
        }

        // 验证手机号格式
        /*if (!phoneField.getText().matches("1[3-9]\\d{9}")) {
            showAlert("注册失败", "请输入有效的手机号");
            return;
        }*/
        try {
            Long.parseLong(phoneField.getText());
        } catch (NumberFormatException e) {
            showAlert("注册失败", "请输入有效的手机号");
            return;
        }

        // 验证日期


        if (!passwordField.getText().equals(confirmPasswordField.getText())) {
            showAlert("注册失败", "两次输入的密码不一致");
            return;
        }
        //System.out.println("IN");
        Map<String,user> users = new HashMap<>();
        try {
            users = UsersIO.readUsers("users.txt");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 检查手机号是否已注册
        if (users.containsKey(phoneField.getText())) {
            showAlert("注册失败", "该手机号已注册");
            return;
        }

        // 创建用户
        user newUser = new user();
        newUser.setPhoneNumber( phoneField.getText());
        newUser.setName(nameField.getText());
        newUser.setGender(gender);
        newUser.setPassword(passwordField.getText());
        users.put(phoneField.getText(),newUser);
        try {
            UsersIO.writeUsers(users,"users.txt");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        showAlert("注册成功", "注册成功，请登录");

        try {
            navigateTo("../view/LogInView.fxml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    private void handleBack() {
        try {
            navigateTo("../view/LogInView.fxml");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void navigateTo(String fxmlPath) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader();
        Stage stage = (Stage) sureButton.getScene().getWindow();
        Stage stage1 = (Stage) backButton.getScene().getWindow();
        URL url = getClass().getResource(fxmlPath);
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root, 600, 400);
        stage.setScene(scene);
        stage.show();
        stage1.setScene(scene);
        stage1.show();
    }

    private void showAlert(String title, String message) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle(title);
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }


}